//// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest.ui.diff
//
//import com.intellij.collaboration.messages.CollaborationToolsBundle
//import com.intellij.collaboration.ui.codereview.action.ImmutableToolbarLabelAction
//import com.intellij.collaboration.ui.codereview.diff.CodeReviewDiffHandlerHelper
//import com.intellij.collaboration.util.KeyValuePair
//import com.intellij.diff.impl.DiffRequestProcessor
//import com.intellij.diff.tools.combined.CombinedDiffComponentProcessor
//import com.intellij.diff.util.DiffUserDataKeys
//import com.intellij.openapi.actionSystem.ActionManager
//import com.intellij.openapi.components.Service
//import com.intellij.openapi.components.serviceIfCreated
//import com.intellij.openapi.diff.impl.GenericDataProvider
//import com.intellij.openapi.project.Project
//import com.intellij.openapi.util.Disposer
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.ExperimentalCoroutinesApi
//import kotlinx.coroutines.channels.awaitClose
//import kotlinx.coroutines.flow.Flow
//import kotlinx.coroutines.flow.channelFlow
//import kotlinx.coroutines.flow.flatMapLatest
//import kotlinx.coroutines.flow.flowOf
//import com.linqingying.gitcode.api.GCRepositoryCoordinates
//import com.linqingying.gitcode.pullrequest.comment.action.GCPRDiffReviewThreadsReloadAction
//import com.linqingying.gitcode.pullrequest.data.GCPRIdentifier
//import com.linqingying.gitcode.pullrequest.ui.review.GCPRReviewViewModel
//import com.linqingying.gitcode.pullrequest.ui.toolwindow.model.GCPRToolWindowProjectViewModel
//import com.linqingying.gitcode.pullrequest.ui.toolwindow.model.GCPRToolWindowViewModel
//
//@Service(Service.Level.PROJECT)
//internal class GCPRDiffService(private val project: Project, parentCs: CoroutineScope) {
//  private val base = CodeReviewDiffHandlerHelper(project, parentCs)
//
//  fun createDiffRequestProcessor(repository: GCRepositoryCoordinates, pullRequest: GCPRIdentifier): DiffRequestProcessor {
//    val vm = findDiffVm(project, repository, pullRequest)
//    return base.createDiffRequestProcessor(vm, ::createDiffContext)
//  }
//
//  fun createCombinedDiffProcessor(repository: GCRepositoryCoordinates, pullRequest: GCPRIdentifier): CombinedDiffComponentProcessor {
//    val vm = findDiffVm(project, repository, pullRequest)
//    return base.createCombinedDiffModel(vm, ::createDiffContext)
//  }
//
//  private fun createDiffContext(vm: GCPRDiffViewModel): List<KeyValuePair<*>> = buildList {
//    add(KeyValuePair(GCPRDiffViewModel.KEY, vm))
//    add(KeyValuePair(DiffUserDataKeys.DATA_PROVIDER, GenericDataProvider().apply {
//      putData(GCPRDiffViewModel.DATA_KEY, vm)
//      putData(GCPRReviewViewModel.DATA_KEY, vm.reviewVm)
//    }))
//    add(KeyValuePair(DiffUserDataKeys.CONTEXT_ACTIONS,
//                     listOf(ImmutableToolbarLabelAction(CollaborationToolsBundle.message("review.diff.toolbar.label")),
//                            GCPRDiffReviewThreadsReloadAction(),
//                            ActionManager.getInstance().getAction("GitCode.PullRequest.Review.Submit"))))
//  }
//}
//
//@OptIn(ExperimentalCoroutinesApi::class)
//private fun findDiffVm(project: Project, repository: GCRepositoryCoordinates, pullRequest: GCPRIdentifier): Flow<GCPRDiffViewModel?> =
//  project.serviceIfCreated<GCPRToolWindowViewModel>()?.projectVm?.flatMapLatest {
//    if (it?.repository == repository) {
//      it.getDiffViewModelFlow(pullRequest)
//    }
//    else {
//      flowOf(null)
//    }
//  } ?: flowOf(null)
//
//private fun GCPRToolWindowProjectViewModel.getDiffViewModelFlow(pullRequest: GCPRIdentifier): Flow<GCPRDiffViewModel> = channelFlow {
//  val acquisitionDisposable = Disposer.newDisposable()
//  val vm = acquireDiffViewModel(pullRequest, acquisitionDisposable)
//  trySend(vm)
//  awaitClose {
//    Disposer.dispose(acquisitionDisposable)
//  }
//}
